﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    public class VCWindowChange : EditorWindow
    {
        private bool allowSubmit;
        private VCAssetList assetList = new VCAssetList();
        private const string c_defaultDescription = "<enter description here>";
        private VCChangelist changeList = new VCChangelist();
        private string description = string.Empty;
        private VCListControl submitList = new VCListControl();

        private void DoOpen(VCChangelist change, VCAssetList assets)
        {
            this.changeList = change;
            this.description = "<enter description here>";
            this.assetList = null;
            if (change == null)
            {
                VCTask task = VCProvider.Status(assets);
                task.Wait();
                VCAsset.States[] states = new VCAsset.States[] { VCAsset.States.CheckedOutLocal, VCAsset.States.DeletedLocal, VCAsset.States.AddedLocal };
                this.assetList = task.assetList.Filter(false, states);
            }
            else
            {
                VCTask task2 = VCProvider.ChangeDescription(change);
                VCTask task3 = VCProvider.ChangeStatus(change);
                task2.Wait();
                task3.Wait();
                this.description = (task2.text.Length <= 0) ? "<enter description here>" : task2.text;
                this.assetList = task3.assetList;
            }
            this.RefreshList();
        }

        public void OnEnable()
        {
            base.position = new Rect(100f, 100f, 700f, 395f);
            base.minSize = new Vector2(700f, 395f);
            this.submitList.ReadOnly = true;
        }

        private void OnGUI()
        {
            GUILayout.Label("Description", EditorStyles.boldLabel, new GUILayoutOption[0]);
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Height(150f) };
            this.description = EditorGUILayout.TextArea(this.description, options);
            GUILayout.Label("Files", EditorStyles.boldLabel, new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            Rect screenRect = new Rect(6f, 206f, base.position.width - 12f, base.position.height - 248f);
            GUILayout.BeginArea(screenRect);
            GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true) };
            GUILayout.Box(string.Empty, optionArray2);
            GUILayout.EndArea();
            this.submitList.OnGUI(new Rect(screenRect.x + 2f, screenRect.y + 2f, screenRect.width - 4f, screenRect.height - 4f), true);
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if (GUILayout.Button("Cancel", new GUILayoutOption[0]))
            {
                base.Close();
            }
            if (GUILayout.Button("Save", new GUILayoutOption[0]))
            {
                this.Save(false);
            }
            if ((this.allowSubmit && (this.assetList.Count > 0)) && GUILayout.Button("Submit", new GUILayoutOption[0]))
            {
                this.Save(true);
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(12f);
        }

        public static void Open(VCAssetList list, bool submit)
        {
            Open(null, list, submit);
        }

        public static void Open(VCChangelist change, VCAssetList assets, bool submit)
        {
            VCWindowChange window = EditorWindow.GetWindow<VCWindowChange>(true, "Version Control Changelist");
            window.allowSubmit = submit;
            window.DoOpen(change, assets);
        }

        private void RefreshList()
        {
            this.submitList.Clear();
            foreach (VCAsset asset in this.assetList)
            {
                this.submitList.Add(null, asset.prettyPath, asset);
            }
            if (this.assetList.Count == 0)
            {
                VCChangelist change = new VCChangelist("empty change list");
                this.submitList.Add(null, change.description, change).Dummy = true;
            }
            this.submitList.Refresh();
            base.Repaint();
        }

        private void Save(bool submit)
        {
            if (this.description.Trim() == "<enter description here>")
            {
                Debug.LogError("Version control: Please enter a valid change description");
            }
            else
            {
                base.Close();
                EditorApplication.SaveAssets();
                VCProvider.Submit(this.changeList, this.assetList, this.description, !submit).Wait();
                VCDatabase.Invalidate(this.assetList);
            }
        }
    }
}

